using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using OVS.BusinessObjects;

namespace OVS.DAL
{
	public partial class CameraDAO
	{
		public CameraDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Camera> GetCameras()
		{
			try
			{
				List<Camera> lstCameras = new List<Camera>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [came_id],[came_loc],[came_url],[came_desc],[came_nom],[came_use] FROM [Camera]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Camera oCamera = new Camera();
					oCamera.came_id = Convert.ToInt32(oDbDataReader["came_id"]);
					oCamera.came_loc = Convert.ToString(oDbDataReader["came_loc"]);
					oCamera.came_url = Convert.ToString(oDbDataReader["came_url"]);

					if(oDbDataReader["came_desc"] != DBNull.Value)
						oCamera.came_desc = Convert.ToString(oDbDataReader["came_desc"]);

					if(oDbDataReader["came_nom"] != DBNull.Value)
						oCamera.came_nom = Convert.ToString(oDbDataReader["came_nom"]);

					if(oDbDataReader["came_use"] != DBNull.Value)
						oCamera.came_use = Convert.ToBoolean(oDbDataReader["came_use"]);
					lstCameras.Add(oCamera);
				}
				oDbDataReader.Close();
				return lstCameras;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Camera GetCamera(int came_id)
		{
			try
			{
				Camera oCamera = new Camera();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [came_id],[came_loc],[came_url],[came_desc],[came_nom],[came_use] FROM [Camera] WHERE [came_id]=@came_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id",DbType.Int32,came_id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oCamera.came_id = Convert.ToInt32(oDbDataReader["came_id"]);
					oCamera.came_loc = Convert.ToString(oDbDataReader["came_loc"]);
					oCamera.came_url = Convert.ToString(oDbDataReader["came_url"]);

					if(oDbDataReader["came_desc"] != DBNull.Value)
						oCamera.came_desc = Convert.ToString(oDbDataReader["came_desc"]);

					if(oDbDataReader["came_nom"] != DBNull.Value)
						oCamera.came_nom = Convert.ToString(oDbDataReader["came_nom"]);

					if(oDbDataReader["came_use"] != DBNull.Value)
						oCamera.came_use = Convert.ToBoolean(oDbDataReader["came_use"]);
				}
				oDbDataReader.Close();
				return oCamera;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddCamera(Camera oCamera)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [Camera]([came_loc],[came_url],[came_desc],[came_nom],[came_use])VALUES(@came_loc,@came_url,@came_desc,@came_nom,@came_use);SELECT SCOPE_IDENTITY();",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_loc",DbType.String,oCamera.came_loc));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_url",DbType.String,oCamera.came_url));
				if (oCamera.came_desc!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc",DbType.String,oCamera.came_desc));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc",DbType.String,DBNull.Value));
				if (oCamera.came_nom!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom",DbType.String,oCamera.came_nom));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom",DbType.String,DBNull.Value));
				if (oCamera.came_use.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_use",DbType.Boolean,oCamera.came_use));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_use",DbType.Boolean,DBNull.Value));

				return Convert.ToInt32(DbProviderHelper.ExecuteScalar(oDbCommand));
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateCamera(Camera oCamera)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [Camera] SET [came_loc]=@came_loc,[came_url]=@came_url,[came_desc]=@came_desc,[came_nom]=@came_nom,[came_use]=@came_use WHERE [came_id]=@came_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_loc",DbType.String,oCamera.came_loc));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_url",DbType.String,oCamera.came_url));
				if (oCamera.came_desc!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc",DbType.String,oCamera.came_desc));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc",DbType.String,DBNull.Value));
				if (oCamera.came_nom!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom",DbType.String,oCamera.came_nom));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom",DbType.String,DBNull.Value));
				if (oCamera.came_use.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_use",DbType.Boolean,oCamera.came_use));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_use",DbType.Boolean,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id",DbType.Int32,oCamera.came_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveCamera(Camera oCamera)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Camera] WHERE [came_id]=@came_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id",DbType.Int32,oCamera.came_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveCamera(int came_id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Camera] WHERE [came_id]=@came_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id",DbType.Int32,came_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
